﻿#include <iostream>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include "Shader.h"

using namespace std;

int SCREEN_WIDTH  = 800;
int SCREEN_HEIGHT = 600;

void processInput(GLFWwindow* window);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int getMaxVertexAttributes()
{
	int nrAttributes = 0;
	glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
	std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl;
	return nrAttributes;
}

int main()
{
	glfwInit();
	// set var GLFW_CONTEXT_VERSION_MAJOR = 3
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	// set var GLFW_CONTEXT_VERSION_MINOR = 3
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	// only use core profile(3.3, shader), not compatibility profile(1.0+, glBegin/glEnd)
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__ // TARGET_OS_MAC
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif // APPLE

	GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "LearnOpenGL", NULL, NULL);
	if (NULL == window) {
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window); // active OpenGL context
	glfwSetWindowSizeCallback(window, framebuffer_size_callback);

	// init GLAD
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	getMaxVertexAttributes();

	std::string root_path = PROJECT_ROOT_DIR;
	std::string vShaderPath = root_path + "/" + std::string("shader.vs");
	std::string fShaderPath = root_path + "/" + std::string("shader.fs");

	// shaders
	Shader shader(vShaderPath.c_str(), fShaderPath.c_str());

	// set up vertex data
	// -----------------------
	float vertices[] = {
		// 顶点位置          // 颜色
		 0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,  // right
		-0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  // left
		 0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f   // top 
	};

	unsigned int VAO, VBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glBindVertexArray(VAO);

	// 复制顶点数组到缓冲中供OpenGL使用
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// 位置属性(attribute 0)
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	// 颜色属性(attribute 1)
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);

	// view
	glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind
	glBindVertexArray(0); // unbind

	while (!glfwWindowShouldClose(window)) {
		processInput(window);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

#if 0 // exercise2
		float offset = 0.5f;
		shader.setFloat("xDelta", offset);
#endif

		// active shader program
		shader.use();
		//shader.setFloat("someUniform", 1.0f);

		// draw triangle
		glBindVertexArray(VAO);
		glDrawArrays(GL_TRIANGLES, 0, 3);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);

	glfwTerminate();
	return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}

void processInput(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}
